package com.heer.gymnasium.module.fellow.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.order.beans.AddOrderBean;
import com.heer.gymnasium.module.order.beans.OrderResponseBean;
import com.heer.gymnasium.module.order.views.OrderActivity;
import com.heer.gymnasium.module.reservation.views.ReservationActivity;
import com.heer.gymnasium.util.StringUtils;
import com.zhihuiyun.library.switchbutton.OnSwitchListener;
import com.zhihuiyun.library.switchbutton.SwitchButton;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/2/23
 * 发起拼单
 */

public class InitiateFellowActivity extends BaseActivity {

    private static final int RES_ID_LAYOUT = R.layout.layout_initiate_fellow;
    private static final int RES_ID_MONEY = R.id.layout_initiate_fellow_money_tv;
    private static final int RES_ID_NUM = R.id.layout_initiate_fellow_num_et;
    private static final int RES_ID_EXPLAIN = R.id.layout_initiate_fellow_explain_et;
    private static final int RES_ID_NEXT = R.id.layout_initiate_fellow_next_tv;
    private static final int RES_ID_OPEN = R.id.layout_initiate_fellow_switch_button;


    @BindView(RES_ID_NUM)
    EditText numEditText;
    @BindView(RES_ID_EXPLAIN)
    EditText explainEditText;
    @BindView(RES_ID_MONEY)
    TextView moneyTextView;
    @BindView(RES_ID_OPEN)
    SwitchButton switchButton;
    private Activity $this;
    private int totalMoney;
    private boolean isOpen = false;

    @Override
    protected void init() {
        super.init();
        setContentView(RES_ID_LAYOUT);
        $this = this;
    }

    @Override
    protected void initView() {
        super.initView();
        totalMoney = (int) getIntent().getFloatExtra(EXTRA_DATA, 0);
        moneyTextView.setText(totalMoney + "元");
        numEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (totalMoney != 0 && !TextUtils.isEmpty(numEditText.getText())) {
                    float money = totalMoney / StringUtils.toInt(numEditText.getText().toString())
                            + (totalMoney % StringUtils.toInt(numEditText.getText().toString()));
                    moneyTextView.setText(money + "元");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        switchButton.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange(int currentStatus) {
                if (currentStatus == SwitchButton.OPEN) {
                    isOpen = true;
                } else {
                    isOpen = false;
                }
            }
        });
    }


    @OnClick(RES_ID_NEXT)
    public void nextOnClick() {
        if (TextUtils.isEmpty(numEditText.getText().toString().trim()) || StringUtils.toInt(numEditText.getText()) <= 1) {
            ToastUtil.show($this, "拼单人数不能为空或少于2");
            return;
        }
        if (TextUtils.isEmpty(explainEditText.getText().toString().trim())) {
            ToastUtil.show($this, "活动信息不可为空");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(EXTRA_DATA, explainEditText.getText().toString());
        if (isOpen) {
            intent.putExtra(EXTRA_TYPE, "1");
        } else {
            intent.putExtra(EXTRA_TYPE, "0");
        }
        intent.putExtra(EXTRA_ID, numEditText.getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    }

    public static void startActivity(Activity context, int requestCode, float price) {
        Intent mIntent = new Intent(context, InitiateFellowActivity.class);
        mIntent.putExtra(EXTRA_DATA, price);
        context.startActivityForResult(mIntent, requestCode);
    }
}
